package org.zhengquan.controller;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zhengquan.common.JsonData;
import org.zhengquan.dto.PageResult;
import org.zhengquan.pojo.SysRole;
import org.zhengquan.service.SysAclService;
import org.zhengquan.service.SysRoleService;
import org.zhengquan.service.SysUserService;
import org.zhengquan.vo.AclVO;
import org.zhengquan.vo.PageQuery;

import java.util.List;
import java.util.Map;

/**
 * @author zhengquan
 * @date 2017/11/2
 */
@Controller
@RequestMapping("/sys/acl/")
@Slf4j
public class SysAclController {

    @Autowired
    private SysAclService sysAclService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("save.json")
    @ResponseBody
    public JsonData save(AclVO aclVO) {
        sysAclService.save(aclVO);
        return JsonData.success();
    }

    @RequestMapping("update.json")
    @ResponseBody
    public JsonData update(AclVO aclVO) {
        sysAclService.update(aclVO);
        return JsonData.success();
    }

    @RequestMapping("acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam Integer aclId){
        Map<String,Object> map = Maps.newHashMap();
        List<SysRole> roleList = sysRoleService.selectRoleListByAclId(aclId);
        map.put("roles",roleList);
        map.put("users",sysUserService.selectLisyByRoleList(roleList));
        return JsonData.success(map);
    }

    @RequestMapping("page.json")
    @ResponseBody
    public JsonData page(@RequestParam Integer aclModuleId, PageQuery pageQuery){
        PageResult result = sysAclService.selectPageByAclModuleId(aclModuleId,pageQuery);
        return JsonData.success(result);
    }
}
